package portfolio;


import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class TesterMain {


	public static void main(String[] args) {
		
		// Un-comment this constructor to read and write to the same file
		//DatabaseParser xml = new XMLParser(new File("resources/database1.xml"));
		
		// Comment out this one if you un-comment out the above constructor
		DatabaseParser xml = new XMLParser(new File("resources/database.xml"), 
											new File("resources/database1.xml"));
		xml.connectToDatabase();
		xml.readDatabase();
		
		//WebTasks webInterface = new WebTasks(xml);
		//webInterface.sortByDate(true);
		
		Scanner in = new Scanner(System.in);
		
		while(true){
			say("Choose one: \n" +
					"1.) Read the database\n" +
					"2.) Add to the database\n" +
					"3.) Edit the database(contains bugs)\n" +
					"4.) Quit");
			
			
			
			boolean notValid = true;
			while(notValid){
				int choice = Integer.parseInt(in.nextLine());
				switch (choice) {
					case 1:
						readAllPages(xml);
						notValid = false;
						break;
					case 2:
						addToDatabase(in, xml);
						notValid = false;
						break;
					case 3:
						editPage(in, xml);
						notValid = false;
						break;
					case 4: 
						System.exit(0); 
						break;
					default:
						say("Please choose a number between 1 and 4");
						break;
				}
			}
			
			
			
			
			
			
		}
		
		
		/*Page fake = new Page(104,
							"Billy Mays",
							"MP3",
							"billy_mays_here.mp3",
							"billy_mays_here_thumb.jpg",
							"Call Now!",
							new Date(),
							"Camera Crew",
							"Coolest Infomerical Ever",
							"If you call within the next 10 minutes, you get someting free");
		
		//DomXmlExample example = new DomXmlExample();
		
		xml.writeToDatabase(fake);
		
*/
	}
	
	public static void readAllPages(DatabaseParser xml){
		List<Page> book = xml.getAllPages();
		
		Iterator<Page> ninja = book.iterator();
		
		while(ninja.hasNext()){
			say("____________________");
			say(ninja.next().toString());
		}
	}
	
	/*public static void editDatabase(Scanner in, DatabaseParser xml){
		System.out.print("Enter Index #:");
		String iInt = in.nextLine();
		int index = Integer.parseInt(iInt);
		
		System.out.print("\nEnter the Author's Name:");
		String author = in.nextLine();
		
		System.out.print("\nEnter the File Extention:");
		String type = in.nextLine();
		
		System.out.print("\nEnter the URL of the media:");
		String org = in.nextLine();
		
		System.out.print("\nEnter the URL of the thumbnail:");
		String thumb = in.nextLine();
		
		System.out.print("\nEnter the Title:");
		String title = in.nextLine();
		
		System.out.print("\nEnter the Date (MM/DD/YY):");
		String sdate = in.nextLine();
		DateFormat df = new SimpleDateFormat("dd/mm/yy");
		Date date;		
		try {
			date = df.parse(sdate);
		} catch (ParseException e) {
			date = new Date();
		}
		
		System.out.print("\nEnter the medium:");
		String med = in.nextLine();
		
		System.out.print("\nEnter Awards:");
		String awards = in.nextLine();
		
		System.out.print("\nEnter any Comments:");
		String comment = in.nextLine();
		
		Page page = new Page(index, author, type, org, thumb, title, date, med, awards, comment);
		
		if(page.isValid()){
			if(xml.writeToDatabase(page))
				say(page.getIndex() + " was added successfully");
			else
				say(page.getIndex() + " already exists. Was not added.");
		} else {
			say(page.toString() + "\n was not a valid Page. Missing data!");
		}
		
	}*/
	
	public static void addToDatabase(Scanner in, DatabaseParser xml){
		System.out.print("Enter Index #:");
		String iInt = in.nextLine();
		int index = Integer.parseInt(iInt);
		
		System.out.print("\nEnter the Author's Name:");
		String author = in.nextLine();
		
		System.out.print("\nEnter the File Extention:");
		String type = in.nextLine();
		
		System.out.print("\nEnter the URL of the media:");
		String org = in.nextLine();
		
		System.out.print("\nEnter the URL of the thumbnail:");
		String thumb = in.nextLine();
		
		System.out.print("\nEnter the Title:");
		String title = in.nextLine();
		
		System.out.print("\nEnter the Date (MM/DD/YY):");
		String sdate = in.nextLine();
		DateFormat df = new SimpleDateFormat("dd/mm/yy");
		Date date;		
		try {
			date = df.parse(sdate);
		} catch (ParseException e) {
			date = new Date();
		}
		
		System.out.print("\nEnter the medium:");
		String med = in.nextLine();
		
		System.out.print("\nEnter Awards:");
		String awards = in.nextLine();
		
		System.out.print("\nEnter any Comments:");
		String comment = in.nextLine();
		
		Page page = new Page(index, author, type, org, thumb, title, date, med, awards, comment);
		
		if(page.isValid()){
			if(xml.writeToDatabase(page))
				say(page.getIndex() + " was added successfully");
			else
				say(page.getIndex() + " already exists. Was not added.");
		} else {
			say(page.toString() + "\n was not a valid Page. Missing data!");
		}
		
	}
	
	public static void editPage(Scanner in, DatabaseParser xml){
		List<Page> book = xml.getAllPages();
		
		Iterator<Page> pages = book.iterator();
		
		say("Pick one of the following index:");
		
		while(pages.hasNext()){
			Page sheet = pages.next();
			say(new Integer(sheet.getIndex()).toString());
		}
		
		String input = in.nextLine();
		int choice = Integer.parseInt(input);
		
		Page edit = xml.getPage(choice);
		
		if(edit == null){
			say("That page does not exist!");
			return;
		} else {
			say("You are going to edit:");
			say(edit.toString());
		}
		
		say("\nPrevious Artist: " + edit.getOwner());
		System.out.print("New Artist: ");
		String author = in.nextLine();
		if(author.isEmpty())
			author = edit.getOwner();
		
		say("\nPrevious File Type: " + edit.getFileType());
		System.out.print("New File Extention:");
		String type = in.nextLine();
		if(type.isEmpty())
			type = edit.getFileType();
		
		say("\nPrevious URL of original: " + edit.getOriginal());
		System.out.print("New URL of the media:");
		String org = in.nextLine();
		if(org.isEmpty())
			org = edit.getOriginal();
		
		say("\nPrevious URL of thumbnail: " + edit.getThumb());
		System.out.print("New URL of the thumbnail:");
		String thumb = in.nextLine();
		if(thumb.isEmpty())
			thumb = edit.getThumb();
		
		say("\nPrevious Title: " + edit.getTitle());
		System.out.print("New Title:");
		String title = in.nextLine();
		if(title.isEmpty())
			title = edit.getTitle();
		
		if(edit.getDate() == null)
			say("No date!");
		else
			say("\nPrevious Date: " + edit.getDate().toString());
		System.out.print("New Date (MM/DD/YY):");
		String sdate = in.nextLine();
		DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
		Date date;		
		try {
			date = df.parse(sdate);
		} catch (ParseException e) {
			date = edit.getDate();
		}
		
		say("\nPrevious Medium: " + edit.getMedium());
		System.out.print("New medium:");
		String med = in.nextLine();
		if(med.isEmpty())
			med = edit.getMedium();
		
		say("\nPrevioius Awards: " + edit.getAwards());
		System.out.print("New Awards:");
		String awards = in.nextLine();
		if(awards.isEmpty())
			awards = edit.getAwards();
		
		say("\nPrevious Comments: " + edit.getComments());
		System.out.print("New Comments:");
		String comment = in.nextLine();
		if(comment.isEmpty())
			comment = edit.getComments();
		
		Page page = new Page(choice, author, type, org, thumb, title, date, med, awards, comment);
		
		if(page.isValid()){
			if(xml.replacePageAt(page, choice))
				say(page.getIndex() + " was edited successfully");
			else
				say(page.getIndex() + " Errors! Was not edited.");
		} else {
			say(page.toString() + "\n was not a valid Page. Missing data! No changes made.");
		}
		
	}
	
	public static void say(String message){
		System.out.println(message);
	}

}
